package com.cweb.admin.controller;

import com.cweb.framework.base.controller.BaseController;
import com.cweb.framework.pojo.CustomException;
import com.cweb.admin.model.wx.wx_mp_accept_record.service.WxMpAcceptRecordService;
import com.cweb.admin.serivce.wxMp.WeixinMpSerivce;
import com.cweb.admin.controller.param.WxMpVerifyParam;
import com.cweb.admin.serivce.wxMp.WeixinPortalService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 微信相关
 */
@RestController
@RequestMapping("/wx/mp")
public class WxController extends BaseController {
    @Autowired
    private HttpServletResponse httpServletResponse;

    @Autowired
    private WeixinMpSerivce weixinMpSerivce;
    @Autowired
    private WeixinPortalService weixinPortalService;
    @Autowired
    private WxMpAcceptRecordService wxMpAcceptRecordService;

    /**
     * 微信公众号
     * get请求，供给微信端验证接口
     */
    @GetMapping("/portal/{platformId}")
    public String portalGet(@PathVariable Long platformId, WxMpVerifyParam param) throws CustomException {
        log.info("-- 微信公众号接口校验 -- 开始");
        // 校验
        weixinPortalService.wxParamVerify(platformId, param);
        log.info("-- 微信公众号接口校验 -- 结束");
        return param.getEchostr();
    }
    /**
     * 微信公众号
     * get请求，供给微信端验证接口
     */
    @PostMapping("/portal/{platformId}")
    public String portalPost(@PathVariable Long platformId, WxMpVerifyParam param,
                       @RequestBody String requestBody) throws CustomException, IOException {
        log.info("-- 微信公众号消息处理 -- 开始");
        // 校验
        WxMpService wxMpService = weixinPortalService.wxParamVerify(platformId, param);
        // 内容解码
        WxMpXmlMessage inMessage = null;
        if(null == param.getEncrypt_type()){
            inMessage = WxMpXmlMessage.fromXml(requestBody);
        }else if ("aes".equalsIgnoreCase(param.getEncrypt_type())){
            inMessage = WxMpXmlMessage.fromEncryptedXml(requestBody,
                    wxMpService.getWxMpConfigStorage(),
                    param.getTimestamp(), param.getNonce(), param.getMsg_signature());
        }
        // 消息保存
        wxMpAcceptRecordService.saveMpAcceptRecord(platformId, inMessage);

        // 测试被动回复
        String out = WxMpXmlOutMessage.TEXT().content("测试回复")
                .fromUser(inMessage.getToUser()).toUser(inMessage.getFromUser())
                .build().toXml();

        log.info("消息内容：" + inMessage.toString());
        log.info("-- 微信公众号消息处理 -- 结束");
        return out;
    }
}
